﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Gomoku
{
    public class Table
    {
        public enum TableSize : int
        {
            SMALL = 15,
            BIG = 19
        }

        public enum SearchDirection
        {
            UP, UPRIGHT, RIGHT, DOWNRIGHT, DOWN, DOWNLEFT, LEFT, UPLEFT
        }

        public int size;
        private Checker[,] table;

        Texture2D checkerBlack;
        Texture2D checkerWhite;
        Texture2D emptyField;

        public Table(TableSize size)
        {
            try
            {
                this.size = (int)size;
                this.table = new Checker[this.size, this.size];
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void initField()
        {
            if (emptyField != null)
            {
                Random r = new Random();
                float fieldWidth = GlobalGameConstants.Scale * this.emptyField.Width;
                float fieldHeight = GlobalGameConstants.Scale * this.emptyField.Height;

                float startX = 60.0f;
                float startY = 60.0f;
                Vector2 position = new Vector2(startX, startY);

                FieldType[] types = { FieldType.BLACK, FieldType.EMPTY, FieldType.WHITE };

                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        table[i, j] = new Checker(new Vector2(position.X, position.Y), types[1],i,j);
                        position.X += fieldWidth;
                    }
                    position.X = startX;
                    position.Y += fieldHeight;
                }
            }
            else
                throw new ContentNotIninitializedException();
        }

        public void loadContent(ContentManager Content)
        {
            this.checkerBlack = Content.Load<Texture2D>("Images\\checkerBlack");
            this.checkerWhite = Content.Load<Texture2D>("Images\\checkerWhite");
            this.emptyField = Content.Load<Texture2D>("Images\\emptyField");

            initField();
        }

        public void draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    switch (table[i, j].type)
                    {
                        case FieldType.BLACK:
                            spriteBatch.Draw(checkerBlack, table[i, j].position, null, Color.White, 0, Vector2.Zero, GlobalGameConstants.Scale, SpriteEffects.None, 1.0f);
                            break;
                        case FieldType.WHITE:
                            spriteBatch.Draw(checkerWhite, table[i, j].position, null, Color.White, 0, Vector2.Zero, GlobalGameConstants.Scale, SpriteEffects.None, 1.0f);
                            break;
                        case FieldType.EMPTY:
                            spriteBatch.Draw(emptyField, table[i, j].position, null, Color.White, 0, Vector2.Zero, GlobalGameConstants.Scale, SpriteEffects.None, 1.0f);
                            break;

                    }
                }
            }

        }

        public Checker putChecker(MouseState mouseState, FieldType type)
        {

            var mousePosition = new Point(mouseState.X, mouseState.Y);

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    Checker currentField = table[i, j];
                    Rectangle area = new Rectangle(
                        (int)currentField.position.X,
                        (int)currentField.position.Y,
                        (int)(emptyField.Width * GlobalGameConstants.Scale),
                        (int)(emptyField.Height * GlobalGameConstants.Scale)
                        );

                    if (area.Contains(mousePosition) && currentField.type == FieldType.EMPTY)
                    {
                        currentField.type = type;
                        return table[i, j];
                    }
                }
            }
            return null;
        }

        public Checker putAIChecker(Vector2 position,FieldType type)
        {
            table[(int)position.Y, (int)position.X].type = type;
            return table[(int)position.Y, (int)position.X];
        }



        public Boolean checkState(int i, int j, FieldType currentCheckerType) //checks if table contains 5 checkers in a row
        {            
            int right=0, left=0, up=0, down=0;
            int upright=0, upleft=0, downright=0, downleft=0;

            if (j + 5 <= size)
            {  //poziomo w prawo
                for (int k = 1; k < 5; k++)
                {
                    if (table[i, j + k].type == currentCheckerType){
                        right++;
                    }
                    else
                    {
                        break;                        
                    }
                }
            }
                      

            if (j - 4 >=0)
            {  //poziomo w lewo
                for (int k = 1; k < 5; k++)
                {
                    if (table[i, j - k].type == currentCheckerType)
                    {
                        left++;
                    }
                    else
                    {
                        break;
                    }
                }
            }            

            if (i + 5 <=size) //pionowo w dol
            {  
                for (int k = 1; k < 5; k++)
                {
                    if (table[i + k, j].type == currentCheckerType)
                    {
                        down++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (i - 4 >= 0) //pionowo w gore
            {
                for (int k = 1; k < 5; k++)
                {
                    if (table[i - k, j].type == currentCheckerType)
                    {
                        up++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (i - 4 >= 0 && j + 5 <= size ) //gora-prawo
            {
                for (int k = 1; k < 5; k++)
                {
                    if (table[i - k, j+k].type == currentCheckerType)
                    {
                        upright++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (i + 5 <= size && j + 5 <= size ) //dol-prawo
            {
                for (int k = 1; k < 5; k++)
                {
                    if (table[i + k, j + k].type == currentCheckerType)
                    {
                        downright++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (i + 5 <= size && j -4 >=0  ) //dol-lewo
            {
                for (int k = 1; k < 5; k++)
                {
                    if (table[i + k , j - k].type == currentCheckerType)
                    {
                        downleft++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (i - 4 >= 0 && j - 4 >= 0) //gora-lewo
            {
                for (int k = 1; k < 5; k++)
                {
                    if (table[i - k, j - k].type == currentCheckerType)
                    {
                        upleft++;
                    }
                    else
                    {
                        break;
                    }
                }
            }


            if (right + left + 1 == 5 || up + down + 1 == 5 || upright+downleft +1 ==5 || upleft + downright +1 ==5)
                return true;


            return false;



        }

        private void searchForPossibilities(Vector2 position, SearchDirection direction, ref int counter, ref FieldType lastField)
        {
            if (position.X < size && position.X > 0 && position.Y < size && position.Y > 0)
            {
                if (table[(int)position.Y, (int)position.X].type == FieldType.BLACK)
                {
                    counter++;
                    switch (direction)
                    {
                        case SearchDirection.UP:
                            searchForPossibilities(new Vector2(position.X, position.Y - 1), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.UPRIGHT:
                            searchForPossibilities(new Vector2(position.X + 1, position.Y - 1), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.RIGHT:
                            searchForPossibilities(new Vector2(position.X + 1, position.Y), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.DOWNRIGHT:
                            searchForPossibilities(new Vector2(position.X + 1, position.Y + 1), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.DOWN:
                            searchForPossibilities(new Vector2(position.X, position.Y + 1), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.DOWNLEFT:
                            searchForPossibilities(new Vector2(position.X - 1, position.Y + 1), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.LEFT:
                            searchForPossibilities(new Vector2(position.X - 1, position.Y), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.UPLEFT:
                            searchForPossibilities(new Vector2(position.X - 1, position.Y - 1), direction, ref counter, ref lastField);
                            break;
                    }
                }
                else if (table[(int)position.Y, (int)position.X].type == FieldType.EMPTY)
                    lastField = FieldType.EMPTY;
                else
                    lastField = FieldType.WHITE;
            }
            else
                lastField = FieldType.WHITE;
        }

        private void searchForThreats(Vector2 position, SearchDirection direction, ref int counter, ref FieldType lastField)
        {
            if (position.X < size && position.X >= 0 && position.Y < size && position.Y >= 0)
            {
                if (table[(int)position.Y, (int)position.X].type == FieldType.WHITE)
                {
                    counter++;
                    switch (direction)
                    {
                        case SearchDirection.UP:
                            searchForThreats(new Vector2(position.X, position.Y - 1), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.UPRIGHT:
                            searchForThreats(new Vector2(position.X + 1, position.Y - 1), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.RIGHT:
                            searchForThreats(new Vector2(position.X + 1, position.Y), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.DOWNRIGHT:
                            searchForThreats(new Vector2(position.X + 1, position.Y + 1), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.DOWN:
                            searchForThreats(new Vector2(position.X, position.Y + 1), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.DOWNLEFT:
                            searchForThreats(new Vector2(position.X - 1, position.Y + 1), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.LEFT:
                            searchForThreats(new Vector2(position.X - 1, position.Y), direction, ref counter, ref lastField);
                            break;
                        case SearchDirection.UPLEFT:
                            searchForThreats(new Vector2(position.X - 1, position.Y - 1), direction, ref counter, ref lastField);
                            break;
                    }
                }
                else if (table[(int)position.Y, (int)position.X].type == FieldType.EMPTY)
                    lastField = FieldType.EMPTY;
                else
                    lastField = FieldType.BLACK;
            }
            else
                lastField = FieldType.BLACK;
        }


        private void fillCounts(int counter, ref Dictionary<AIPlayer.ThreatsAndPossibilities, int> threatsAndPossibilities, FieldType lastField, int threatsOrPossibilities)
        {                                                                                                           //checks counter and last field and sets right counter in threatsAndPossibilities
            switch(threatsOrPossibilities)
            {
                case 0:                         //Threats
                    if (counter == 2)
                    {
                        if (lastField == FieldType.EMPTY)
                            threatsAndPossibilities[AIPlayer.ThreatsAndPossibilities.ENEMYO2]++;
                        else if (lastField == FieldType.BLACK)
                            threatsAndPossibilities[AIPlayer.ThreatsAndPossibilities.ENEMYC2]++;
                    }
                    else if (counter == 3)
                    {
                        if (lastField == FieldType.EMPTY)
                            threatsAndPossibilities[AIPlayer.ThreatsAndPossibilities.ENEMYO3]++;
                        else if (lastField == FieldType.BLACK)
                            threatsAndPossibilities[AIPlayer.ThreatsAndPossibilities.ENEMYC3]++;
                    }
                    else if (counter == 4)
                    {
                        if (lastField == FieldType.BLACK)
                            threatsAndPossibilities[AIPlayer.ThreatsAndPossibilities.ENEMYC4]++;
                    }
                    break;
                case 1:                         //Possibilities
                    if (counter == 2)
                    {
                        if (lastField == FieldType.EMPTY)
                            threatsAndPossibilities[AIPlayer.ThreatsAndPossibilities.SELFO2]++;
                        else if (lastField == FieldType.WHITE)
                            threatsAndPossibilities[AIPlayer.ThreatsAndPossibilities.SELFC2]++;
                    }
                    else if (counter == 3)
                    {
                        if (lastField == FieldType.EMPTY)
                            threatsAndPossibilities[AIPlayer.ThreatsAndPossibilities.SELFO3]++;
                        else if (lastField == FieldType.WHITE)
                            threatsAndPossibilities[AIPlayer.ThreatsAndPossibilities.SELFC3]++;
                    }
                    else if (counter == 4)
                    {
                        threatsAndPossibilities[AIPlayer.ThreatsAndPossibilities.SELF4]++;
                    }
                    break;
            }
        }

        public void checkTable(Vector2 position, ref Dictionary<AIPlayer.ThreatsAndPossibilities,int> threatsAndPossibilities)          //searches table for threats and possobilities and fills counts in threatsAndPossibilities dictionary
        {
            int counter;
            int upThreatCounter = 0, upRightThreatCounter = 0, rightThreatCounter = 0, downRightThreatCounter = 0, downThreatCounter = 0,
                downLeftThreatCounter = 0, leftThreatCounter = 0, upLeftThreatCounter = 0;
            int upPossibilitiesCounter = 0, upRightPossibilitiesCounter = 0, rightPossibilitiesCounter = 0, downRightPossibilitiesCounter = 0,
                downPossibilitiesCounter = 0, downLeftPossibilitiesCounter = 0, leftPossibilitiesCounter = 0, upLeftPossibilitiesCounter = 0;
            FieldType upThreatLastField = FieldType.NONE, upRightThreatLastField = FieldType.NONE, rightThreatLastField = FieldType.NONE,
                downRightThreatLastField = FieldType.NONE, downThreatLastField = FieldType.NONE, downLeftThreatLastField = FieldType.NONE,
                leftThreatLastField = FieldType.NONE, upLeftThreatLastField = FieldType.NONE;
            FieldType upPossibilitiesLastField = FieldType.NONE, upRightPossibilitiesLastField = FieldType.NONE, rightPossibilitiesLastField = FieldType.NONE,
                downRightPossibilitiesLastField = FieldType.NONE, downPossibilitiesLastField = FieldType.NONE, downLeftPossibilitiesLastField = FieldType.NONE,
                leftPossibilitiesLastField = FieldType.NONE, upLeftPossibilitiesLastField = FieldType.NONE;

            searchForThreats(new Vector2(position.X, position.Y - 1), SearchDirection.UP, ref upThreatCounter, ref upThreatLastField);      //call searchForThreats for all directions from specified point
            searchForThreats(new Vector2(position.X + 1, position.Y - 1), SearchDirection.UPRIGHT, ref upRightThreatCounter, ref upRightThreatLastField);
            searchForThreats(new Vector2(position.X + 1, position.Y), SearchDirection.RIGHT, ref rightThreatCounter, ref rightThreatLastField);
            searchForThreats(new Vector2(position.X + 1, position.Y + 1), SearchDirection.DOWNRIGHT, ref downRightThreatCounter, ref downRightThreatLastField);
            searchForThreats(new Vector2(position.X, position.Y + 1), SearchDirection.DOWN, ref downThreatCounter, ref downThreatLastField);
            searchForThreats(new Vector2(position.X - 1, position.Y + 1), SearchDirection.DOWNLEFT, ref downLeftThreatCounter, ref downLeftThreatLastField);
            searchForThreats(new Vector2(position.X - 1, position.Y), SearchDirection.LEFT, ref leftThreatCounter, ref leftThreatLastField);
            searchForThreats(new Vector2(position.X - 1, position.Y - 1), SearchDirection.UPLEFT, ref upLeftThreatCounter, ref upLeftThreatLastField);

            searchForPossibilities(new Vector2(position.X, position.Y - 1), SearchDirection.UP, ref upPossibilitiesCounter, ref upPossibilitiesLastField);      //call searchForPossibilities for all directions from specified point
            searchForPossibilities(new Vector2(position.X + 1, position.Y - 1), SearchDirection.UPRIGHT, ref upRightPossibilitiesCounter, ref upRightPossibilitiesLastField);
            searchForPossibilities(new Vector2(position.X + 1, position.Y), SearchDirection.RIGHT, ref rightPossibilitiesCounter, ref rightPossibilitiesLastField);
            searchForPossibilities(new Vector2(position.X + 1, position.Y + 1), SearchDirection.DOWNRIGHT, ref downRightPossibilitiesCounter, ref downRightPossibilitiesLastField);
            searchForPossibilities(new Vector2(position.X, position.Y + 1), SearchDirection.DOWN, ref downPossibilitiesCounter, ref downPossibilitiesLastField);
            searchForPossibilities(new Vector2(position.X - 1, position.Y + 1), SearchDirection.DOWNLEFT, ref downLeftPossibilitiesCounter, ref downLeftPossibilitiesLastField);
            searchForPossibilities(new Vector2(position.X - 1, position.Y), SearchDirection.LEFT, ref leftPossibilitiesCounter, ref leftPossibilitiesLastField);
            searchForPossibilities(new Vector2(position.X - 1, position.Y - 1), SearchDirection.UPLEFT, ref upLeftPossibilitiesCounter, ref upLeftPossibilitiesLastField);

            //UP CHECK POSSIBILITIES
            if (upPossibilitiesCounter > 0 && downPossibilitiesCounter > 0)
            {
                counter = upPossibilitiesCounter + downPossibilitiesCounter;
                if ((upPossibilitiesLastField == FieldType.BLACK || downPossibilitiesLastField == FieldType.BLACK) && upPossibilitiesLastField != downPossibilitiesLastField)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.BLACK, 1);
                }
                else if (upPossibilitiesLastField == FieldType.EMPTY && downPossibilitiesLastField == FieldType.EMPTY)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.EMPTY, 1);
                }
            }
            else if (upPossibilitiesCounter == 0 && upPossibilitiesCounter != downPossibilitiesCounter)
            {
                if (downPossibilitiesLastField == FieldType.BLACK)
                    fillCounts(downPossibilitiesCounter, ref threatsAndPossibilities, FieldType.BLACK, 1);
                else if (downPossibilitiesLastField == FieldType.EMPTY)
                    fillCounts(downPossibilitiesCounter, ref threatsAndPossibilities, FieldType.EMPTY, 1);
            }
            else if (downPossibilitiesCounter == 0 && upPossibilitiesCounter != downPossibilitiesCounter)
            {
                if (upPossibilitiesLastField == FieldType.BLACK)
                    fillCounts(upPossibilitiesCounter, ref threatsAndPossibilities, FieldType.BLACK, 1);
                else if (upPossibilitiesLastField == FieldType.EMPTY)
                    fillCounts(upPossibilitiesCounter, ref threatsAndPossibilities, FieldType.EMPTY, 1);
            }

            //UP RIGHT CHECK POSSIBILITIES
            if (upRightPossibilitiesCounter > 0 && downLeftPossibilitiesCounter > 0)
            {
                counter = upRightPossibilitiesCounter + downLeftPossibilitiesCounter;
                if ((upRightPossibilitiesLastField == FieldType.BLACK || downLeftPossibilitiesLastField == FieldType.BLACK) && upRightPossibilitiesLastField != downLeftPossibilitiesLastField)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.BLACK, 1);
                }
                else if (upRightPossibilitiesLastField == FieldType.EMPTY && downLeftPossibilitiesLastField == FieldType.EMPTY)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.EMPTY, 1);
                }
            }
            else if (upRightPossibilitiesCounter == 0 && upRightPossibilitiesCounter != downLeftPossibilitiesCounter)
            {
                if (downLeftPossibilitiesLastField == FieldType.BLACK)
                    fillCounts(downLeftPossibilitiesCounter, ref threatsAndPossibilities, FieldType.BLACK, 1);
                else if (downLeftPossibilitiesLastField == FieldType.EMPTY)
                    fillCounts(downLeftPossibilitiesCounter, ref threatsAndPossibilities, FieldType.EMPTY, 1);
            }
            else if (downLeftPossibilitiesCounter == 0 && upRightPossibilitiesCounter != downLeftPossibilitiesCounter)
            {
                if (upRightPossibilitiesLastField == FieldType.BLACK)
                    fillCounts(upRightPossibilitiesCounter, ref threatsAndPossibilities, FieldType.BLACK, 1);
                else if (upRightPossibilitiesLastField == FieldType.EMPTY)
                    fillCounts(upRightPossibilitiesCounter, ref threatsAndPossibilities, FieldType.EMPTY, 1);
            }

            //RIGHT-LEFT CHECK POSSIBILITIES
            if (rightPossibilitiesCounter > 0 && leftPossibilitiesCounter > 0)
            {
                counter = rightPossibilitiesCounter + leftPossibilitiesCounter;
                if ((rightPossibilitiesLastField == FieldType.BLACK || leftPossibilitiesLastField == FieldType.BLACK) && rightThreatLastField != leftPossibilitiesLastField)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.BLACK, 1);
                }
                else if (rightPossibilitiesLastField == FieldType.EMPTY && leftPossibilitiesLastField == FieldType.EMPTY)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.EMPTY, 1);
                }
            }
            else if (rightPossibilitiesCounter == 0 && rightPossibilitiesCounter != leftPossibilitiesCounter)
            {
                if (leftPossibilitiesLastField == FieldType.BLACK)
                    fillCounts(leftPossibilitiesCounter, ref threatsAndPossibilities, FieldType.BLACK, 1);
                else if (leftPossibilitiesLastField == FieldType.EMPTY)
                    fillCounts(leftPossibilitiesCounter, ref threatsAndPossibilities, FieldType.EMPTY, 1);
            }
            else if (leftPossibilitiesCounter == 0 && rightPossibilitiesCounter != leftPossibilitiesCounter)
            {
                if (rightPossibilitiesLastField == FieldType.BLACK)
                    fillCounts(rightPossibilitiesCounter, ref threatsAndPossibilities, FieldType.BLACK, 1);
                else if (rightPossibilitiesLastField == FieldType.EMPTY)
                    fillCounts(rightPossibilitiesCounter, ref threatsAndPossibilities, FieldType.EMPTY, 1);
            }

            //UP LEFT CHECK POSSIBILITIES
            if (upLeftPossibilitiesCounter > 0 && downRightPossibilitiesCounter > 0)
            {
                counter = upLeftPossibilitiesCounter + downRightPossibilitiesCounter;
                if ((upLeftPossibilitiesLastField == FieldType.BLACK || downRightPossibilitiesLastField == FieldType.BLACK) && upLeftPossibilitiesLastField != downRightPossibilitiesLastField)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.BLACK, 1);
                }
                else if (upLeftPossibilitiesLastField == FieldType.EMPTY && downRightPossibilitiesLastField == FieldType.EMPTY)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.EMPTY, 1);
                }
            }
            else if (upLeftPossibilitiesCounter == 0 && upLeftPossibilitiesCounter != downRightPossibilitiesCounter)
            {
                if (downRightPossibilitiesLastField == FieldType.BLACK)
                    fillCounts(downRightPossibilitiesCounter, ref threatsAndPossibilities, FieldType.BLACK, 1);
                else if (downRightPossibilitiesLastField == FieldType.EMPTY)
                    fillCounts(downRightPossibilitiesCounter, ref threatsAndPossibilities, FieldType.EMPTY, 1);
            }
            else if (downRightPossibilitiesCounter == 0 && upLeftPossibilitiesCounter != downRightPossibilitiesCounter)
            {
                if (upLeftPossibilitiesLastField == FieldType.BLACK)
                    fillCounts(upLeftPossibilitiesCounter, ref threatsAndPossibilities, FieldType.BLACK, 1);
                else if (upLeftPossibilitiesLastField == FieldType.EMPTY)
                    fillCounts(upLeftPossibilitiesCounter, ref threatsAndPossibilities, FieldType.EMPTY, 1);
            }




            //UP CHECK THREATS
            if (upThreatCounter > 0 && downThreatCounter > 0)
            {
                counter = upThreatCounter + downThreatCounter;
                if ((upThreatLastField == FieldType.BLACK || downThreatLastField == FieldType.BLACK) && upThreatLastField != downThreatLastField)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.BLACK, 0);
                }
                else if (upThreatLastField == FieldType.EMPTY && downThreatLastField == FieldType.EMPTY)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.EMPTY, 0);
                }
            }
            else if (upThreatCounter == 0 && upThreatCounter != downThreatCounter)
            {
                if (downThreatLastField == FieldType.BLACK)
                    fillCounts(downThreatCounter, ref threatsAndPossibilities, FieldType.BLACK, 0);
                else if (downThreatLastField == FieldType.EMPTY)
                    fillCounts(downThreatCounter, ref threatsAndPossibilities, FieldType.EMPTY, 0);
            }
            else if (downThreatCounter == 0 && upThreatCounter != downThreatCounter)
            {
                if (upThreatLastField == FieldType.BLACK)
                    fillCounts(upThreatCounter, ref threatsAndPossibilities, FieldType.BLACK, 0);
                else if (upThreatLastField == FieldType.EMPTY)
                    fillCounts(upThreatCounter, ref threatsAndPossibilities, FieldType.EMPTY, 0);
            }

            //UP RIGHT CHECK THREATS
            if (upRightThreatCounter > 0 && downLeftThreatCounter > 0)
            {
                counter = upRightThreatCounter + downLeftThreatCounter;
                if ((upRightThreatLastField == FieldType.BLACK || downLeftThreatLastField == FieldType.BLACK) && upRightThreatLastField != downLeftThreatLastField)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.BLACK, 0);
                }
                else if (upRightThreatLastField == FieldType.EMPTY && downLeftThreatLastField == FieldType.EMPTY)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.EMPTY, 0);
                }
            }
            else if (upRightThreatCounter == 0 && upRightThreatCounter != downLeftThreatCounter)
            {
                if (downLeftThreatLastField == FieldType.BLACK)
                    fillCounts(downLeftThreatCounter, ref threatsAndPossibilities, FieldType.BLACK, 0);
                else if (downLeftThreatLastField == FieldType.EMPTY)
                    fillCounts(downLeftThreatCounter, ref threatsAndPossibilities, FieldType.EMPTY, 0);
            }
            else if (downLeftThreatCounter == 0 && upRightThreatCounter != downLeftThreatCounter)
            {
                if (upRightThreatLastField == FieldType.BLACK)
                    fillCounts(upRightThreatCounter, ref threatsAndPossibilities, FieldType.BLACK, 0);
                else if (upRightThreatLastField == FieldType.EMPTY)
                    fillCounts(upRightThreatCounter, ref threatsAndPossibilities, FieldType.EMPTY, 0);
            }

            //RIGHT-LEFT CHECK THREATS
            if (rightThreatCounter > 0 && leftThreatCounter > 0)
            {
                counter = rightThreatCounter + leftThreatCounter;
                if ((rightThreatLastField == FieldType.BLACK || leftThreatLastField == FieldType.BLACK) && rightThreatLastField != leftThreatLastField)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.BLACK, 0);
                }
                else if (rightThreatLastField == FieldType.EMPTY && leftThreatLastField == FieldType.EMPTY)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.EMPTY, 0);
                }
            }
            else if (rightThreatCounter == 0 && rightThreatCounter != leftThreatCounter)
            {
                if (leftThreatLastField == FieldType.BLACK)
                    fillCounts(leftThreatCounter, ref threatsAndPossibilities, FieldType.BLACK, 0);
                else if (leftThreatLastField == FieldType.EMPTY)
                    fillCounts(leftThreatCounter, ref threatsAndPossibilities, FieldType.EMPTY, 0);
            }
            else if (leftThreatCounter == 0 && rightThreatCounter != leftThreatCounter)
            {
                if (rightThreatLastField == FieldType.BLACK)
                    fillCounts(rightThreatCounter, ref threatsAndPossibilities, FieldType.BLACK, 0);
                else if (rightThreatLastField == FieldType.EMPTY)
                    fillCounts(rightThreatCounter, ref threatsAndPossibilities, FieldType.EMPTY, 0);
            }

            //UP LEFT CHECK THREATS
            if (upLeftThreatCounter > 0 && downRightThreatCounter > 0)
            {
                counter = upLeftThreatCounter + downRightThreatCounter;
                if ((upLeftThreatLastField == FieldType.BLACK || downRightThreatLastField == FieldType.BLACK) && upLeftThreatLastField != downRightThreatLastField)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.BLACK, 0);
                }
                else if (upLeftThreatLastField == FieldType.EMPTY && downRightThreatLastField == FieldType.EMPTY)
                {
                    fillCounts(counter, ref threatsAndPossibilities, FieldType.EMPTY, 0);
                }
            }
            else if (upLeftThreatCounter == 0 && upLeftThreatCounter != downRightThreatCounter)
            {
                if (downRightThreatLastField == FieldType.BLACK)
                    fillCounts(downRightThreatCounter, ref threatsAndPossibilities, FieldType.BLACK, 0);
                else if (downRightThreatLastField == FieldType.EMPTY)
                    fillCounts(downRightThreatCounter, ref threatsAndPossibilities, FieldType.EMPTY, 0);
            }
            else if (downRightThreatCounter == 0 && upLeftThreatCounter != downRightThreatCounter)
            {
                if (upLeftThreatLastField == FieldType.BLACK)
                    fillCounts(upLeftThreatCounter, ref threatsAndPossibilities, FieldType.BLACK, 0);
                else if (upLeftThreatLastField == FieldType.EMPTY)
                    fillCounts(upLeftThreatCounter, ref threatsAndPossibilities, FieldType.EMPTY, 0);
            }
        }

        public bool isPlaceEmpty(Vector2 position)              //checks if place in table is empty
        {
            if (table[(int)position.Y, (int)position.X].type == FieldType.EMPTY)
                return true;
            else
                return false;
        }
    }
}
